package com.sunxueqi.project.tool.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sunxueqi.framework.web.vo.ResponseData;
import com.sunxueqi.project.tool.service.IGenerateService;
import com.sunxueqi.project.tool.vo.BeanField;
import com.sunxueqi.project.tool.vo.GenerateDetail;
import com.sunxueqi.project.tool.vo.GenerateInput;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="代码生成")
@RestController
@RequestMapping("/tool/generate")
public class GenerateController {

	@Autowired
	private IGenerateService generateService;
	
	@ApiOperation("获取所有的数据表名称")
	@GetMapping("/getTables")
	public ResponseData getTables() {
		List<String> tables = generateService.getTables();
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		map.put("tableName", tables);
		return ResponseData.success(map);
	} 
	
	/**
	 * 获取数据表信息
	 * @param tableName --数据表名
	 * @return
	 */
	@ApiOperation("获取数据表信息")
	@GetMapping("/getTableField/{tableName}")
	public ResponseData getTableField(@PathVariable String tableName) {
		GenerateDetail detail = new GenerateDetail();
		detail.setBeanName(StrUtil.upperFirst(StrUtil.toCamelCase(tableName)));
		List<BeanField> fields = generateService.listBeanField(tableName);
		detail.setFields(fields);
		return ResponseData.success(detail);
	}
	
	@ApiOperation("代码生成")
	@PostMapping("/saveCode")
	public ResponseData saveCode(@RequestBody GenerateInput input) {
		generateService.generateCode(input);
		return ResponseData.success();
	}
}
